//
//  GKInteractiveTransitionTool.m
//  GKTranstionAnimation
//
//  Created by Apple on 16/11/16.
//  Copyright © 2016年 郭凯. All rights reserved.
//

#import "GKInteractiveTransitionTool.h"
@interface GKInteractiveTransitionTool()
/**手势方向*/
@property (nonatomic, assign) InteractiveTransitionGestureDirection direction;
/**手势类型*/
@property (nonatomic, assign) InteractiveTransitionType type;
/**当前的控制器*/
@property (nonatomic, weak) UIViewController *vc;
@end

@implementation GKInteractiveTransitionTool


//类方法
+(instancetype)interactiveTransitionWithTransitionType:(InteractiveTransitionType)type GestureDirection:(InteractiveTransitionGestureDirection)direction{
    
    return [[self alloc]initWithTransitionType:type GestureDirection:direction];
}
//构造方法
-(instancetype)initWithTransitionType:(InteractiveTransitionType)type GestureDirection:(InteractiveTransitionGestureDirection)direction{
    self = [super init];
    if (self) {
        _direction = direction;
        _type = type;
    }
    return self;
}

//给控制器视图添加手势
-(void)addPanGestureForViewController:(UIViewController *)viewController{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    self.vc = viewController;
    [viewController.view addGestureRecognizer:pan];
}

// 手势处理
-(void)handleGesture:(UIPanGestureRecognizer *)panGesture{
    //手势百分比
    CGFloat persent = 0;
    
    NSLog(@"当前的状态 = %ld",panGesture.state);
    switch (_direction) {
        case InteractiveTransitionGestureDirectionLeft:{
            CGFloat transitionX = -[panGesture translationInView:panGesture.view].x;
            persent = transitionX / panGesture.view.frame.size.width;
        }
            break;
        case InteractiveTransitionGestureDirectionRight:{
            CGFloat transitionX = [panGesture translationInView:panGesture.view].x;
            persent = transitionX / panGesture.view.frame.size.width;
        }
            break;
        case InteractiveTransitionGestureDirectionUp:{
            CGFloat transitionY = -[panGesture translationInView:panGesture.view].y;
            persent = transitionY / panGesture.view.frame.size.width;
        }
            break;
        case InteractiveTransitionGestureDirectionDown:{
            CGFloat transitionY = [panGesture translationInView:panGesture.view].y;
            persent = transitionY / panGesture.view.frame.size.width;
            NSLog(@"当前的 = %lf == %lf",transitionY,persent);
        }
            break;
            
    }
    
    
    switch (panGesture.state) {
        case UIGestureRecognizerStateBegan://开始拖动
            self.interation = YES;
            [self startGesture];
            break;
        case UIGestureRecognizerStateChanged://正在拖动
            [self updateInteractiveTransition:persent];
            break;
        case UIGestureRecognizerStateEnded://结束拖动
            self.interation = NO;
            if (persent>0.5) {
                [self finishInteractiveTransition];
            }else{
                [self cancelInteractiveTransition];
            }
            break;
        default:
            break;
    }
}



- (void)startGesture{
    switch (_type) {
        case InteractiveTransitionTypePresent:{
            if (_presentConifg) {
                _presentConifg();
            }
        }
            break;
            
        case InteractiveTransitionTypeDismiss:
            [_vc dismissViewControllerAnimated:YES completion:nil];
            break;
        case InteractiveTransitionTypePush:{
            if (_pushConifg) {
                _pushConifg();
            }
        }
            break;
        case InteractiveTransitionTypePop:
            [_vc.navigationController popViewControllerAnimated:YES];
            break;
    }
}
@end
